package ua.kharkov.knure.mikhaylov.st4.web.command;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import ua.kharkov.knure.mikhaylov.st4.db.entities.Account;
import ua.kharkov.knure.mikhaylov.st4.db.entities.User;
import ua.kharkov.knure.mikhaylov.st4.db.management.DBManager;
import ua.kharkov.knure.mikhaylov.st4.settings.Path;

/**
 * A command which describes the process of payment creation. During the
 * creation it has to be cast 3 times. Every time it processes the next
 * information segment. The code is hatred because of a lack of js knowledge
 * 
 * @author Saint
 * 
 */
public class ContinueCreatePaymentCommand extends Command {

	// BAD CODE HERE

	// WARNING: THIS CODE MAY BE HARMFUL

	private static final long serialVersionUID = 8888006328770313702L;
	private static final Logger log = Logger
			.getLogger(ContinueCreatePaymentCommand.class);

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {

		log.debug("Command starts");

		String forward = Path.PAGE_CREATE_PAYMENT;
		HttpSession session = request.getSession();

		// user was changed
		if (request.getParameter("setUser") != null) {
			// set target user [targetUser]
			User targetUser = DBManager.getInstance().findUserByLogin(
					request.getParameter("user"));
			session.setAttribute("targetUser", targetUser);
			log.trace("Target user has been set -->" + targetUser);
			// set target user's accounts [senderAccounts]
			List<Account> senderAccounts = DBManager.getInstance()
					.findOpenAccountsByUser(targetUser);
			session.setAttribute("senderAccounts", senderAccounts);
			log.trace("Sender accounts have been set --> "
					+ senderAccounts.size());
			// unset sender account
			session.setAttribute("senderAccount", null);
			log.trace("Sender account was unset");
			// unset reciever accounts
			session.setAttribute("recieverAccounts", null);
			log.trace("Reciever accounts was unset");
			// unset reciever account
			session.setAttribute("recieverAccount", null);
			log.trace("Reciever accounts were unset");
		}
		// sender account was changed
		else if (request.getParameter("setSenderAcc") != null) {
			// set sender account
			Account senderAccount = DBManager.getInstance().findAccountById(
					Long.valueOf(request.getParameter("senderAccount")),
					(User) session.getAttribute("targetUser"));
			session.setAttribute("senderAccount", senderAccount);
			log.trace("Sender account has been set --> " + senderAccount);
			// set reciever accounts
			List<Account> recieverAccounts = DBManager.getInstance()
					.findOpenAccountsByUserAndValuta(
							(User) session.getAttribute("user"),
							senderAccount.getValuta());
			session.setAttribute("recieverAccounts", recieverAccounts);
			log.trace("Reciever accounts have been set --> "
					+ recieverAccounts.size());
			// unset reciever account
			session.setAttribute("recieverAccount", null);
			log.trace("Reciever account has been unset");
		}
		// reciever account was changed
		else if (request.getParameter("setRecieverAcc") != null) {
			// set reciever account
			Account recieverAccount = DBManager.getInstance().findAccountById(
					Long.valueOf(request.getParameter("recieverAccount")),
					(User) request.getSession().getAttribute("user"));
			session.setAttribute("recieverAccount", recieverAccount);
			log.trace("Reciever account has been set --> " + recieverAccount);
		}
		// confirmation
		else if (request.getParameter("makePayment") != null) {
			// set forward
			Double value = Double.valueOf(request.getParameter("value"));
			session.setAttribute("value", value);
			log.trace("Payment value --> " + value);
			forward = Path.COMMAND_CREATE_PAYMENT;
		}

		log.debug("Command finished");
		return forward;
	}

}
